package oracle.springbootxm.controller;

import oracle.springbootxm.pojo.GoodsInfo;
import oracle.springbootxm.pojo.UserGoodsInfo;
import oracle.springbootxm.pojo.UserInfo;
import oracle.springbootxm.service.GoodsService;
import oracle.springbootxm.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class GoodsController {
    @Autowired
    UserService userService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/addCar")
    @ResponseBody
    public String addCar(UserInfo userInfo, GoodsInfo goodsInfo, HttpSession session) {
        if (session.getAttribute("userInfo") == null) {
            return "aaa";
        } else {
            int userid = userInfo.getUserid();//报错：userid一直取值为0；但数据库为1；且页面显示也是0； 原因：userInfo对象来源不明。
            int goodsid = goodsInfo.getGoodsid();
            System.out.println(userid + "......" + goodsid);
            UserGoodsInfo userInfo1 = goodsService.queryuserGoods(userid, goodsid);
            Map map = new HashMap();
            int count = 1;
            map.put("userid", userid);
            map.put("goodsid", goodsInfo.getGoodsid());
            GoodsInfo goodsInfo1 = new GoodsInfo();
            if (userInfo1 == null) {
                map.put("usergoodscount", count);
                goodsService.addGoods(map);
                GoodsInfo goodsInfo2 = goodsService.queryshowGoods(goodsInfo.getGoodsid());
                goodsInfo2.setGoodscount(count);
                List list = (List) session.getAttribute("usergoods");
                list.add(goodsInfo2);
                System.out.println(list.size());
            } else {
                count = 1 + userInfo1.getUsergoodscount();
                goodsService.updateCount(count, goodsid);
                List list = (List) session.getAttribute("usergoods");
                goodsInfo1 = (GoodsInfo) list.get(goodsid - 1);
                goodsInfo1.setGoodscount(count);
                System.out.println(goodsInfo1.getGoodscount());
            }
            return "添加成功";
        }
    }

    @RequestMapping("/jump")
    public String jump(HttpSession session) {
        List<GoodsInfo> list = goodsService.queryGoodsInfoAll();
        System.out.println(list.get(0).getGoodsname());
        session.setAttribute("list", list);
        System.out.println(session.getAttribute("userInfo"));
        if (session.getAttribute("userInfo") == null) {
            System.out.println("qqq");
            return "index";
        } else {
            System.out.println("aaa");
            return "/shop";
        }
    }

    @RequestMapping("/jumpmygoods")
    public String jumpmygoods(HttpSession session) {
        if (session.getAttribute("userInfo") == null) {
            return "login";
        } else {
            return "mygoods";
        }
    }

    @RequestMapping("/shop")
    public String goodsShop(HttpSession session) {//userInfo对象中只有username;所以userid=0
        UserInfo userinfo = (UserInfo) session.getAttribute("userInfo");
        System.out.println(userinfo.getUserid());
        List<UserGoodsInfo> goodsInfo1 = goodsService.queryuserAllGoods(userinfo.getUserid());
        if (goodsInfo1 != null) {
            List<GoodsInfo> usergoods = new ArrayList<GoodsInfo>();
            for (int i = 0; i < goodsInfo1.size(); i++) {
                GoodsInfo goodsInfo = goodsService.queryshowGoods(goodsInfo1.get(i).getGoodsid());
                goodsInfo.setGoodscount(goodsInfo1.get(i).getUsergoodscount());
                usergoods.add(goodsInfo);
            }
            session.setAttribute("usergoods", usergoods);
            session.setAttribute("abc", userinfo);
        }

        return "index";
    }

    @RequestMapping("/jumpindex")
    public String backIndex() {
        return "index";
    }

    @RequestMapping("/pay")
    @ResponseBody
    public String paymoney(GoodsInfo goodsInfo,HttpSession session) {
        System.out.println(goodsInfo.getGoodsname());
        System.out.println(goodsInfo.getGoodspic());
        System.out.println(goodsInfo.getGoodsprice());
        System.out.println(goodsInfo.getGoodscount());
        GoodsInfo paygoods = new GoodsInfo();
        paygoods.setGoodsprice(goodsInfo.getGoodsprice());
        session.setAttribute("paygoods",paygoods);
        return "paymoney";
    }

    @RequestMapping("/paypage")
    public String paypage() {
        return "paymoney";
    }

    @RequestMapping("/goodsdetails")
    @ResponseBody
    public String goGoodsdetails(GoodsInfo goodsInfo, HttpSession session) {
        if (session.getAttribute("userInfo") == null) {
            System.out.println("未登录");
            return "login";
        } else {
            System.out.println("已登录");
            session.setAttribute("goodsInfo1", goodsInfo);
            System.out.println(goodsInfo.getGoodsname());
            return "goodsdetails";
        }

    }

    @RequestMapping("/goodsdetailspage")
    public String pageGoodsdetails() {
        return "goodsdetails";
    }

    @RequestMapping("/nowbuy")
    public String nowBuygoods(GoodsInfo goodsInfo, Model model) {
        model.addAttribute("buygoods", goodsInfo);
        return "立即购买";
    }

    @RequestMapping("/jumpnowbuy")
    public String jumpNowBuygoods() {
        return "paymoney";
    }

    @RequestMapping("/deleteGoods")
    @ResponseBody
    public String deleteGoods(GoodsInfo goodsInfo, UserInfo userInfo, HttpSession session) {
        System.out.println("delete");
        goodsService.deleteGoods(userInfo.getUserid(), goodsInfo.getGoodsid());
        List list = (List) session.getAttribute("usergoods");
        list.remove(goodsInfo.getGoodsid() - 1);
        return "删除商品";
    }

    @RequestMapping("/addgoods")
    public String addGoods(){
        return "addgoods";
    }

    @RequestMapping("/deleteStoreGoods")
    @ResponseBody
    public String deleteStoreGoods(GoodsInfo goodsInfo,HttpSession session) {
        System.out.println("delete");
        goodsService.deleteStoreGoods(goodsInfo.getGoodsid());
        List list = (List) session.getAttribute("list");
        list.remove(goodsInfo.getGoodsid() - 1);
        return "删除商品";
    }

    @RequestMapping("/paysuccess")
    public String paysucccess() {
        return "paysucess";
    }

}
